<template>
  <div class="station">
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      ref="stationRule"
      :rules="rules"
    >
      <el-form-item label="车站名" prop="station">
        <el-input v-model="formInline.station" placeholder="车站名"></el-input>
      </el-form-item>
      <el-form-item label="车站状态">
        <el-select v-model="formInline.status" placeholder="活动区域" @change="onStatusChange">
          <el-option label="正常" value="0"></el-option>
          <el-option label="停用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('stationRule')">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="create('create')">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column fixed prop="stationNum" label="车站标识"></el-table-column>
      <el-table-column prop="stationName" label="车站名" width="200"></el-table-column>
      <el-table-column prop="bindCity" label="所在城市"></el-table-column>
      <el-table-column prop="bindProvince" label="所在省份"></el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button
            @click="handleClick(scope.row)"
            type="text"
            size="small"
          >{{scope.row.status===0?'删除':'恢复'}}</el-button>
          <el-button @click="handleEdit(scope.row, 'edit')" type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="提示" :visible.sync="centerDialogVisible" width="40vw" center>
      <el-form ref="createForm" :model="form" :rules="crules" label-width="80px">
        <el-form-item label="车站名称" prop="stationName">
          <el-input v-model="form.stationName"></el-input>
        </el-form-item>
        <el-form-item label="绑定省份" prop="province">
          <el-select v-model="form.province" placeholder="选择省份">
            <el-option label="河北" value="河北"></el-option>
            <el-option label="河南" value="河南"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="绑定城市" prop="city">
          <el-select v-model="form.city" placeholder="选择城市">
            <el-option label="石家庄" value="石家庄"></el-option>
            <el-option label="周口" value="周口"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeDialogVisible('createForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "Station",

  data() {
    const checkStation = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入车站名"));
      }
      const regPos = /\d+/;
      const regNeg = /[a-zA-Z]+/;
      if (regPos.test(value) || regNeg.test(value)) {
        callback(new Error("请不要输入站点名！"));
      } else {
        callback();
      }
    };
    return {
      row: {},
      option: "",
      form: {
        stationName: "",
        province: "",
        city: ""
      },
      tableData: [],
      total: 0,
      formInline: {
        station: "",
        status: ""
      },
      centerDialogVisible: false,
      rules: {
        station: [{ required: true, message: "请输入车站名" }]
      },
      crules: {
        stationName: [{ validator: checkStation, trigger: "blur" }],
        province: [{ required: true, message: "请输入绑定省份" }],
        city: [{ required: true, message: "请输入绑定城市" }]
      }
    };
  },
  created() {},
  mounted() {
    this.initData();
  },
  methods: {
    async initData() {
      const res = await this.$http.get("/admin/station/stationList");
      if (res.data.code === 0) {
        this.total = res.data.data.count;
        this.tableData = res.data.data.stationList;
      }
    },
    changeDialogVisible(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          if (this.option === "create") {
            const res = await this.$http.post("/admin/station/save", {
              stationName: this.form.stationName,
              bindProvince: this.form.province,
              bindCity: this.form.city
            });
            if (res.data.code === 0) {
              this.centerDialogVisible = false;
              this.initData();
            }
          } else {
            const res = await this.$http.post("/admin/station/edit", {
              stationId: this.row.stationId,
              stationName: this.form.stationName,
              bindProvince: this.form.province,
              bindCity: this.form.city,
              stationNum: this.row.stationNum
            });
            if (res.data.code === 0) {
              this.centerDialogVisible = false;
              this.initData();
            }
          }
        } else {
          return false;
        }
      });
    },
    create(opt) {
      this.centerDialogVisible = true;
      this.option = opt;
    },
    async handleClick(row) {
      if (row.status === 0) {
        this.$confirm("站点将被停用，是否继续？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(async () => {
          const res = await this.$http.get(
            `/admin/station/delete?stationId=${row.stationId}`
          );
          if (res.data.code === 0) {
            this.initData();
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          }
        });
      } else {
        this.$confirm("站点将被恢复，是否继续？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(async () => {
          const res = await this.$http.get(
            `/admin/station/recover?stationId=${row.stationId}`
          );
          if (res.data.code === 0) {
            this.initData();
            this.$message({
              type: "success",
              message: "恢复成功!"
            });
          }
        });
      }
    },
    handleEdit(row, opt) {
      this.option = opt;
      this.centerDialogVisible = true;
      this.form.stationName = row.stationName;
      this.form.province = row.bindProvince;
      this.form.city = row.bindCity;
      this.row = row;
    },
    onSubmit(formName) {
      // this.$refs[formName].validate(valid => {
      //   if (valid) {
      //     this.$http
      //       .get(
      //         `/admin/station/findByName?stationName=${this.formInline.station}`
      //       )
      //       .then(res => {
      //         if (res.data.code === 0) {
      //           this.tableData = res.data.data;
      //         }
      //       });
      //   } else {
      //     return false;
      //   }
      // });
      window.console.log(formName)
      this.$http
        .get(`/admin/station/findByName?stationName=${this.formInline.station}`)
        .then(res => {
          if (res.data.code === 0) {
            this.tableData = res.data.data;
          }
        });
    },

    async onStatusChange() {
      const res = await this.$http.get(
        `/admin/station/findByStatus?status=${this.formInline.status}`
      );
      if (res.data.code === 0) {
        this.tableData = res.data.data;
      }
    }
  }
};
</script>
<style lang="less" scoped>
</style>
